package com.example.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commonutils.R;
import com.example.music.mapper.CommentMapper;
import com.example.music.model.domain.Comment;
import com.example.music.model.request.CommentRequest;
import com.example.music.model.request.UserSupportRequest;
import com.example.music.service.CommentService;
import com.example.music.service.UserSupportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    UserSupportService userSupportService;

    @Override
    public R addComment(CommentRequest addCommentRequest) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(addCommentRequest, comment);
        comment.setType(addCommentRequest.getNowType());
        if (commentMapper.insert(comment) > 0) {
            return R.success("评论成功");
        } else {
            return R.error("评论失败");
        }
    }

    @Override
    public R updateCommentMsg(CommentRequest addCommentRequest) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(addCommentRequest, comment);
        if (commentMapper.updateById(comment) > 0) {
            return R.success("点赞成功");
        } else {
            return R.error("点赞失败");
        }
    }

    @Override
    public R commentLike(UserSupportRequest userLike) {
        Long count = userSupportService.selectRecord(userLike);
        if (count == 0) {
            Comment comment = commentMapper.selectById(userLike.getCommentId());
            comment.setUp(comment.getUp() + 1);
            commentMapper.updateById(comment);
            userSupportService.insertCommentSupport(userLike);
            return R.success("点赞成功");
        } else {
            Comment comment = commentMapper.selectById(userLike.getCommentId());
            comment.setUp(comment.getUp() - 1);
            commentMapper.updateById(comment);
            userSupportService.deleteCommentSupport(userLike);
            return R.success("取消点赞");
        }

    }

    //    删除评论
    @Override
    public R deleteComment(Integer id) {
        if (commentMapper.deleteById(id) > 0) {
            userSupportService.deleteAllComment(id);
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R commentOfSongId(Integer songId, Integer userId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_id", songId);
        queryWrapper.orderByDesc("up");
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        System.out.println(comments);
        Map<String,Object> map= new HashMap<>();
        map.put("comments",comments);
        List<Integer> list = new ArrayList<>();
        if (userId!=null){
            list = userSupportService.selectUserId(userId);
        }
        map.put("upId",list);
        return R.success(null, map);
    }

    @Override
    public R commentOfSongListId(Integer songListId, Integer userId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_list_id", songListId);
        queryWrapper.orderByDesc("up");
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        Map<String,Object> map= new HashMap<>();
        map.put("comments",comments);
        List<Integer> list = new ArrayList<>();
        if (userId!=null){
            list = userSupportService.selectUserId(userId);
        }
        map.put("upId",list);
        return R.success(null, map);
    }
}
